package com.ddyx.common.utils.network;


import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * httpClient工具类
 */
@Slf4j
public class HttpClient {

    /**
     * 接收http请求处理
     * @param request
     * @param content 内容-谁发的通知
     * @return
     */
    public static String reciveHttpReq(HttpServletRequest request, String content) throws Exception {

        String resMes = "";
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(), "GBK"));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            resMes = sb.toString();
            log.info(content+"[----->]" + resMes);
        } catch (IOException e) {
            e.printStackTrace();
            throw new Exception("接收http请求处理异常");
        }
        return resMes;
    }

    public static String reciveHttpReq2(HttpServletRequest request, String content) throws Exception {

        String resMes = "";
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(), "utf-8"));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            resMes = sb.toString();
            log.info(content+"[----->]" + resMes);
        } catch (IOException e) {
            e.printStackTrace();
            throw new Exception("接收http请求处理异常");
        }
        return resMes;
    }

}
